/*******************************************************************

    Author: Xiao
    Date: 2022/3/29 17:24

*******************************************************************/
package dao

type Room struct {
	RoomId           int         `gorm:"primarykey" json:"room_id" binding:"required"`
	RoomUser         int         `json:"room_user" `
	RoomAddsongcd    int         `json:"room_addsongcd" `
	RoomAddcount     int         `json:"room_addcount" `
	RoomPushdaycount int         `json:"room_pushdaycount" `
	RoomPushsongcd   int         `json:"room_pushsongcd" `
	RoomOnline       int         `json:"room_online"`
	RoomRealonline   int         `json:"room_realonline"`
	RoomHide         int         `json:"room_hide" `
	RoomName         string      `json:"room_name" binding:"required"`
	RoomType         int         `json:"room_type" `
	RoomPublic       int         `json:"room_public" `
	RoomPassword     string      `json:"room_password"`
	RoomNotice       string      `json:"room_notice"`
	RoomAddsong      int         `json:"room_addsong"`
	RoomSendmsg      int         `json:"room_sendmsg"`
	RoomRobot        int         `json:"room_robot" `
	RoomOrder        int         `json:"room_order" `
	RoomReason       string      `json:"room_reason"`
	RoomPlayone      int         `json:"room_playone" `
	RoomVotepass     int         `json:"room_votepass"`
	RoomVotepercent  int         `json:"room_votepercent"`
	RoomBackground   string      `json:"room_background"`
	RoomApp          string      `json:"room_app"`
	RoomFullpage     int         `json:"room_fullpage"`
	RoomStatus       int         `json:"room_status"`
	RoomCreatetime   int64       `gorm:"autoCreateTime" json:"room_createtime"`
	RoomUpdatetime   int64       `gorm:"autoUpdateTime" json:"room_updatetime"`
	Admin            interface{} `json:"admin" gorm:"-"`
}

// 通过用户Id获取房间
func GetRoomByUserId(uid int) (Room, error) {
	var r Room
	result := MDB.Where("room_user = ?", uid).First(&r)
	return r, result.Error
}

// 通过Id获取房间
func GetRoomById(id int) (Room, error) {
	var r Room
	result := MDB.Where("room_id = ?", id).First(&r)
	return r, result.Error
}

// 更新房间信息
func UpdateRoom(rid int, column map[string]interface{}) error {
	result := MDB.Model(&Room{}).Where("room_id = ?", rid).Updates(column)
	return result.Error
}

// 创建房间
func CreateRoom(r Room) (Room, error) {
	result := MDB.Select("room_user", "room_name", "room_notice", "room_type").Create(&r)
	return r, result.Error
}

type HostRoom struct {
	Room
	UserId    int    `json:"user_id"`
	UserName  string `json:"user_name"`
	UserGroup int    `json:"user_group"`
	UserHead  string `json:"user_head"`
	UserAdmin bool   `json:"user_admin"`
}

// 获取热门房间
func GetHotRooms() ([]HostRoom, error) {
	var rs []HostRoom
	result := MDB.Model(&Room{}).Select("sa_room.*",
		"sa_user.user_id", "sa_user.user_name", "sa_user.user_head", "sa_user.user_group").Joins(
		"left join sa_user on sa_room.room_user = sa_user.user_id").Where(
		"(sa_room.room_online > 0 or sa_room.room_order > 1000000) and sa_room.room_hide = 0").Order(
		"sa_room.room_order desc,sa_room.room_online desc,sa_room.room_id asc").Find(&rs)
	return rs, result.Error
}

// 获取有人的房间
func GetRooms() []Room {
	var rs []Room
	MDB.Model(&Room{}).Omit("room_createtime", "room_updatetime").Where(
		"room_type in (1,4) and room_realonline > 0").Find(&rs)
	return rs
}
